// ///////////////////////////// MIT License //////////////////////////////////// //
//                                                                                //
// Copyright (c) 2010 David Zsolt Manrique                                        //
//                    david.zsolt.manrique@gmail.com                              //
//                                                                                //
// Permission is hereby granted, free of charge, to any person obtaining a copy   //
// of this software and associated documentation files (the "Software"), to deal  //
// in the Software without restriction, including without limitation the rights   //
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      //
// copies of the Software, and to permit persons to whom the Software is          //
// furnished to do so, subject to the following conditions:                       //
//                                                                                //
// The above copyright notice and this permission notice shall be included in     //
// all copies or substantial portions of the Software.                            //
//                                                                                //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    //
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN      //
// THE SOFTWARE.                                                                  //
//                                                                                //
// ////////////////////////////////////////////////////////////////////////////// //

// standard headers
/*
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <iomanip>
#include <cmath>
#include <cstdarg>
#include <stdexcept>
#include <utility>
#include <tuple>

#include <cmdioutil.h>*/
#include "param_interpret.h"
#include "xyz.h"


int main(int argc, char** argv)
{
try
{   
	cmd::arguments arg(argc,argv);
	cmd::switcharg<false> help(arg,"-h","--help", "print help message");
        cmd::switcharg<false> stdio(arg,"-std","--std", "standard input and output");
        cmd::switcharg<false> print_out(arg,"-p","--print", "print out result (and no overwrite)");

        xyz c;
        if(*stdio) load(std::cin,c);

        cmd::vararg<std::string> output_fn (arg,"-o","--output","ouput file name","output");
//	cmd::vararg<util::list_expression<int> > selected_range (arg,"-s","--select","select atoms by i1:i2,i3 range, default all selected","");
        cmd::multivararg<std::string> selected_range (arg,"-s","--select","(repeatable opt) select atoms, by default all selected");
        
        cmd::switcharg<false> invert_selection(arg,"-i","--invert-selection", "invert selection range");
        //////////////////////////////////////////////////////////////////////////

        cmd::vararg<std::string> translate_tO (arg,"-tO","--tO","translate @1 co. into O origin","");
        cmd::vararg<std::string> translate_t (arg,"-t","--t","translate by @1 co.","");
        cmd::varargxx<std::string,std::string> translate_tto (arg,"-tto","--tto","translate @1 co. to @2","","");
        cmd::vararg<std::string> translate_tC (arg,"-tC","--t-copy","copy & translate by @1 co.","");

        cmd::vararg<std::string> translate_tx (arg,"-tx","--tx","translate by &1 along x","");
        cmd::vararg<std::string> translate_ta1 (arg,"-ta1","--ta1","translate by &1 along a1","");
        cmd::vararg<std::string> translate_ua1 (arg,"-ua1","--ua1","change a1 length by &1","");
        cmd::vararg<std::string> translate_tua1 (arg,"-tua1","--tua1","-ta1 & -ua1","");

        cmd::vararg<std::string> translate_ty (arg,"-ty","--ty","translate by &1 along y","");
        cmd::vararg<std::string> translate_ta2 (arg,"-ta2","--ta2","translate by &1 along a2","");
        cmd::vararg<std::string> translate_ua2 (arg,"-ua2","--ua2","change a2 length by &1","");
        cmd::vararg<std::string> translate_tua2 (arg,"-tua2","--tua2","-ta2 & -ua2","");
        
        cmd::vararg<std::string> translate_tz (arg,"-tz","--tz","translate by &1 along z","");
        cmd::vararg<std::string> translate_ta3 (arg,"-ta3","--ta3","translate by &1 along a3","");
        cmd::vararg<std::string> translate_ua3 (arg,"-ua3","--ua3","change a3 length by &1","");
        cmd::vararg<std::string> translate_tua3 (arg,"-tua3","--tua3","-ta3 & -ua3","");
                
        ///////////////////////////////////////////////////////////////////////////
        std::string fn;
        if(*help || !*stdio)
        {
	        cmd::posarg<std::string> io_fn(arg,"input file name", "");
                if(!*help) { fn = *io_fn; load(fn,c); }
        }
	
        
	if(*help)
	{
		arg.print_help(std::cout,"xyztr");
                std::cout << std::endl;
                std::cout << interpret_expression_help();                
                std::cout << std::endl;
                std::cout << "examples:" << std::endl;
                std::cout << "   xyztr m.xyz -t 0/0/1          #translate by 0/0/1 coord "<< std::endl;
                std::cout << "   xyztr m.xyz -s 1,3:9 -t 0/0/1 #translate 1,3..9 atoms by 0/0/1 coord "<< std::endl;
                std::cout << "   xyztr m.xyz -tO 4             #translate in a way that atom 4 goes to origin"<< std::endl;
                std::cout << "   xyztr m.xyz -t 3%8            #translate in a way that atom 3 goes to atom 8 position"<< std::endl;
                std::cout << "   xyztr m.xyz -tC a1            #copy & translate into to the next unit cell along a1"<< std::endl;
                std::cout << std::endl;
                std::cout << "By default m.xyz will be overwritten!"<< std::endl;
                std::cout << std::endl;
                std::cout << "Version 0.9b"<< std::endl;
                std::cout << "Copyright (c) 2010 David Zsolt Manrique (david.zsolt.manrique@gmail.com)"<< std::endl;
                
                
		return EXIT_SUCCESS;
	}
        ///////////////////////////////////////////////////////////////////////////
	//if(~selected_range) select(c,*selected_range); else neg(c);
        if(~selected_range) for(int i=0;i<(*selected_range).size();i++) select_by_expression(c,(*selected_range)[i]); else neg(c);
        if(*invert_selection) neg(c);
        ////////////////////////////////////////////////////

        Eigen::Vector3d a1,a2,a3,na1,na2,na3;
        a1 = c.lattice[0];
        a2 = c.lattice[1];
        a3 = c.lattice[2];
        na1 = a1.normalized();
        na2 = a2.normalized();
        na3 = a3.normalized();
        
        if(~translate_tO) { Eigen::Vector3d d = interpret_coord(c,*translate_tO); c-=d;
        } else if (~translate_t) {  Eigen::Vector3d d =interpret_coord(c,*translate_t); c+=d;
        } else if (~translate_tto) {  Eigen::Vector3d d = interpret_coord(c,translate_tto.var2)-interpret_coord(c,translate_tto.var1); c+=d;
        } else if (~translate_tC) { xyz c2 = c; Eigen::Vector3d d = interpret_coord(c,*translate_tC); c2+=d; pour(c,c2); 
        } else if (~translate_tx) {  Eigen::Vector3d d(interpret_disp(c,*translate_tx),0.,0.); c +=d;
        } else if (~translate_ta1) { Eigen::Vector3d d=na1*interpret_disp(c,*translate_ta1);  c += d;
        } else if (~translate_ua1) { Eigen::Vector3d d=na1*interpret_disp(c,*translate_ua1);  c.lattice[0] += d;
        } else if (~translate_tua1) { Eigen::Vector3d d=na1*interpret_disp(c,*translate_tua1);  c += d; c.lattice[0] += d;
        } else if (~translate_ty) { Eigen::Vector3d d(0.,interpret_disp(c,*translate_ty),0.); c +=d;
        } else if (~translate_ta2) { Eigen::Vector3d d=na2*interpret_disp(c,*translate_ta2);  c += d;
        } else if (~translate_ua2) { Eigen::Vector3d d=na2*interpret_disp(c,*translate_ua2);  c.lattice[1] += d;
        } else if (~translate_tua2) { Eigen::Vector3d d=na2*interpret_disp(c,*translate_tua2);  c += d; c.lattice[1] += d;
        } else if (~translate_tz) { Eigen::Vector3d d(0.,0.,interpret_disp(c,*translate_tz)); c +=d; 
        } else if (~translate_ta3) { Eigen::Vector3d d=na3*interpret_disp(c,*translate_ta3);  c += d;
        } else if (~translate_ua3) { Eigen::Vector3d d=na3*interpret_disp(c,*translate_ua3);  c.lattice[2] += d;
        } else if (~translate_tua3) {Eigen::Vector3d d=na3*interpret_disp(c,*translate_tua3);  c += d; c.lattice[2] += d;
        }
        
        ////////////////////////////////////////////////////
        if(*print_out) print(std::cout,c);
        else if(*stdio) save(std::cout,c);
        else if(~output_fn) save(*output_fn,c);
        else save(fn,c);

} catch(const char* message)
{
	std::cerr << message << std::endl;
	std::cerr << "(exit)" << std::endl;
	return EXIT_FAILURE;
} 
catch(const std::exception & e)
{
        std::cerr << e.what() << std::endl;
        std::cerr << "(exit)" << std::endl;
        return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}


